package com.polaris.live.utils

import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.polaris.live.common.mvvm.fragment.BaseVmFragment
import com.polaris.live.common.mvvm.fragment.FragmentLifecycleEventObserver
import com.polaris.live.common.util.ifDestroy

/**
 * XPopupUtils
 *
 * @author Created by lucas on 2023/11/2 19:55
 */
object XPopupUtils {

    /**
     * 显示已view显示的popup，并且没有阴影
     */
    fun showViewPopup(
        popupView: BasePopupView,
        builder: (XPopup.Builder.() -> XPopup.Builder)? = null,
        isDisposable: Boolean = true,
        lifecycle: Lifecycle? = null
    ) {
        val popup = XPopup.Builder(popupView.context)
            .isViewMode(true)
            .hasShadowBg(false)
            .isDestroyOnDismiss(isDisposable)
            .run {
                if (builder != null) {
                    builder()
                } else {
                    this
                }
            }
            .asCustom(popupView)
            .show()

        if (lifecycle != null) {
            popup.autoDestroy(lifecycle)
        }
    }

    /**
     * 显示一个popup，并且没有阴影
     */
    fun showPopup(
        popupView: BasePopupView,
        builder: (XPopup.Builder.() -> XPopup.Builder)? = null,
        isDisposable: Boolean = true,
        lifecycle: Lifecycle? = null
    ) {
        val popup = XPopup.Builder(popupView.context)
            .hasShadowBg(false)
            .isDestroyOnDismiss(isDisposable)
            .run {
                if (builder != null) {
                    builder()
                } else {
                    this
                }
            }
            .asCustom(popupView)
            .show()

        if (lifecycle != null) {
            popup.autoDestroy(lifecycle)
        }
    }
}

/**
 * 如果lifecycle销毁则自动dismiss
 */
fun BasePopupView.autoDestroy(ownerLifecycle: Lifecycle) {
    val l = LifecycleEventObserver { _, event ->
        if (event == Lifecycle.Event.ON_DESTROY) {
            dismiss()
        }
    }
    lifecycle.ifDestroy {
        ownerLifecycle.removeObserver(l)
    }
    ownerLifecycle.addObserver(l)
}

/**
 * 当是fragment时，自动隐藏
 */
fun BasePopupView.autoFragmentHide(owner: Fragment) {
    if (owner is BaseVmFragment<*>) {
        val l = object : FragmentLifecycleEventObserver {
            override fun onHiddenChanged(hidden: Boolean) {
                if (hidden) {
                    dismiss()

                    owner.removeFragmentLifecycleEventObserver(this)
                }
            }
        }
        lifecycle.ifDestroy {
            owner.removeFragmentLifecycleEventObserver(l)
        }
        owner.addFragmentLifecycleEventObserver(l)
    }
}

/**
 * 当是fragment时，自动隐藏
 */
fun DialogFragment.autoFragmentHide(owner: Fragment) {
    if (owner is BaseVmFragment<*>) {
        val l = object : FragmentLifecycleEventObserver {
            override fun onHiddenChanged(hidden: Boolean) {
                if (hidden) {
                    if (fragmentManager != null) {
                        if (isAdded && !isStateSaved) {
                            dismiss()
                        } else {
                            dismissAllowingStateLoss()
                        }
                    }

                    owner.removeFragmentLifecycleEventObserver(this)
                }
            }
        }
        lifecycle.ifDestroy {
            owner.removeFragmentLifecycleEventObserver(l)
        }
        owner.addFragmentLifecycleEventObserver(l)
    }
}